package battleship.view.fieldpainters;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import javax.swing.JLabel;

import battleship.PluginAI;
import battleship.PluginSetup;
import battleship.Setup;
import battleship.boat.Boat;
import battleship.field.Field;
import battleship.player.Player;
import battleship.view.FieldPainter;

@SuppressWarnings("serial")
public class FieldPainterSetup extends FieldPainter{
	
	private Point currentHighlighted = null;
	private Point currentClicked = null;
	private Boat[] boats;
	private int currentBoat;
	private boolean finished = false;
	
	public FieldPainterSetup(Player owner, JLabel notificationArea )
	{
		super(owner, notificationArea);
	}
	
	public void fetchBoats()
	{
		boats = getFieldOwner().getField().getBoats();
		currentBoat = 0;
	}
	
	public void setFinished(boolean finished)
	{
		this.finished = finished;
	}
	
	public boolean getFinished()
	{
		return finished;
	}
	
	public void paintComponent(Graphics g)
	{
		
		super.paintComponent(g);
		
		Graphics2D g2 = (Graphics2D) g;
		
		// bot setup
		if(getFieldOwner().isBot())
		{
			setHideShips(true);
			PluginAI ai = getFieldOwner().getAI();
			PluginSetup setup = ai.getSetupAlgorithm();
			
			for (int boat = 0; boat < getFieldOwner().getField().getBoats().length; ++boat)
			{
				setup.setupExecFunction(getFieldOwner(), getFieldOwner().getField().getBoats()[boat], null);
			}
		}
		
		//human setup
		else if(!finished)
		{
			if (getLastPointHighlighted() != currentHighlighted)
			{
				int length = boats[currentBoat].getLength();
				int x = (int) getLastPointHighlighted().getX();
				int y = (int) getLastPointHighlighted().getY();

				if (getVertical())
				{
					if(y + length > Field.getHeight())
					{
						g2.setColor(Color.RED);
						length = Field.getHeight() - y;
					}
					else if (boats[currentBoat].collisionDetection(
							getLastPointHighlighted().x,
							getLastPointHighlighted().y,
							getVertical(), 
							getFieldOwner()))
					{
						g2.setColor(Color.RED);
					}
					else
					{
						g2.setColor(Color.GRAY);
					}

					g2.fillRect(x*25, y*25, 25, length*25);
					
					g2.setColor(Color.BLACK);
					g2.drawRect(x * 25, y * 25, 25, length * 25);
				}
				else
				{
					if (x + length > Field.getWidth())
					{
						g2.setColor(Color.RED);
						length = Field.getWidth() - x;
					}
					else if (
							boats[currentBoat].collisionDetection(getLastPointHighlighted().x,
							getLastPointHighlighted().y,
							getVertical(),
							getFieldOwner()))
					{
						g2.setColor(Color.RED);
					}
					else
					{
						g2.setColor(Color.GRAY);
					}

					g2.fillRect(x*25, y*25, length*25, 25);
					g2.setColor(Color.BLACK);
					g2.drawRect(x * 25, y * 25, length * 25, 25);
					
					
				}
				
				setNotificationText("Positie: "
						+ Setup.getPositionMap()[x]
						+ ""
						+ (y + 1)
						, false);
				
			}



			if ((getLastPointClicked() != null) && (getLastPointClicked() != currentClicked))
			{
				int x = (int) getLastPointClicked().getX();
				int y = (int) getLastPointClicked().getY();
				
				currentClicked = getLastPointClicked();

				if(boats[currentBoat].setPosition(getFieldOwner(), x, y, getVertical()))
				{	
					
					setNotificationText("boot gezet op positie: "
							+ Setup.getPositionMap()[x]
							                         + ""
							                         + (y + 1), 
							                         true);

					currentBoat ++;

					if(currentBoat >= boats.length)
					{
						finished = true;
					}
				}
			}
		}
	}
}
